﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace IPlugin
{
    public class Response
    {
         //Attribute
        private Socket socket;

        //Properties
        public int StatusCode { get; set; }
        public string StatusText { get; set; }
        public string ContentType { get; set; }
        public string Data { get; set; }
        public bool SendFile { get; set; }
        public string PathToFile { get; set; }

        public Response(Socket socket)
        {
            this.socket = socket;
            SendFile = false;
        }

        public bool sendResponse()
        {
            FileStream fs = null;
            BinaryReader br = null;
            try
            {
                NetworkStream stream = new NetworkStream(socket);
                StreamWriter writer = new StreamWriter(stream);

                if (SendFile)
                {
                    if (File.Exists(PathToFile))
                    {
                        FileInfo fInfo = new FileInfo(PathToFile);
                        long fileLength = fInfo.Length;

                        fs = new FileStream(PathToFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                        br = new BinaryReader(fs);
                        byte[] buffer = new byte[100000]; //100KB lesen

                        writer.WriteLine("HTTP/1.0 " + StatusCode + " " + StatusText);
                        writer.WriteLine("Date: " + DateTime.Now.ToString());
                        writer.WriteLine("Server: Embedded_Sensor_Cloud Webserver v0.1");
                        writer.WriteLine("Content-Length: " + fileLength);
                        writer.WriteLine("Content-Type: " + " x-type/subtype");
                        writer.WriteLine("Content-Disposition: attachment; filename=\"" + Path.GetFileName(PathToFile) + "\"");
                        writer.WriteLine();
                        writer.Flush();

                        int count;
                        while ((count = br.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            stream.Write(buffer, 0, count);
                            stream.Flush();
                        }
                        br.Close();
                        fs.Close();
                        socket.Close();
                    }
                }
                else
                {
                    writer.WriteLine("HTTP/1.0 " + StatusCode + " " + StatusText);
                    writer.WriteLine("Date: " + DateTime.Now.ToString());
                    writer.WriteLine("Server: Embedded_Sensor_Cloud Webserver v0.1");
                    writer.WriteLine("Content-Length: " + Data.Length);
                    writer.WriteLine("Connection: close");
                    writer.WriteLine("Content-Type: " + ContentType);
                    writer.WriteLine();

                    writer.WriteLine(Data);
                    writer.Flush();
                    socket.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                if(socket != null)
                    socket.Close();
                if (fs != null)
                    fs.Close();
                if (br != null)
                    br.Close();
                return false;
            }

            return true;
        }
    }
}
